#include <iostream>
#include <graphics.h>
#include <string>
//#include <algorithm>
//#include <vector>
using namespace std;
int arr[] = {5162,
             8641,
             4923,
             3247
            };
int four_dim_arr[4][4];


int rw[9][2] = {
//	{1*150,1*150},
//	{1*150,2*150},
//	{1*150,3*150},
//	{2*150,1*150},
//	{2*150,2*150},
//	{2*150,3*150},
//	{3*150,1*150},
//	{3*150,2*150},
//	{3*150,3*150},
};
//初始化 rw 数组，计算九宫格中每个点的坐标
void delay_s(int second) {
	delay_ms(1000 * second);
}
void enu() {
//	坐标点
	for (int y = 0; y < 3; y++) {
		for (int x = 0; x < 3; x++) {
			rw[y * 3 + x][0] = (x + 1) * 150;
			rw[y * 3 + x][1] = (y + 1) * 150;

			cout << "{" << rw[y * 3 + x][0] << ", " << rw[y * 3 + x][1] << "}";
		}
		cout << endl << endl;
	}
//	0-8单列显示
	for (int i = 0; i < 9; i++) {
		cout << i;
		cout << "{" << rw[i][0] << ", " << rw[i][1] << "}" << endl;
	}
}
//将 arr 中的四位数拆分，存储到 four_dim_arr 中
void split() {
	for (int i = 0; i < 4; ++i) {
		int temp = arr[i];
		cout << endl;
		cout << "Processing arr[" << i << "] = " << temp << endl;

		for (int j = 3; j >= 0; --j) {
			four_dim_arr[i][j] = temp % 10;
			temp /= 10;
		}
	}
	cout << endl << "Final result:" << endl;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			cout << four_dim_arr[i][j] << " ";
		}
		cout << endl;
	}
}
//初始化
void nine() {
	int num = 1;
	for (int i = 1; i <= 3; i++) {
		//TODO创建行
		for (int j = 1; j <= 3; j++, num++) {
			//TODO创建列
			circle(i * 150, j * 150, 50);
//			line();
			fillcircle(j * 150, i * 150, 25);
			setfont(30, 30, "宋体");
			setbkmode(TRANSPARENT);
//			setfontbkcolor(BLUE);
			settextjustify(CENTER_TEXT, CENTER_TEXT);
			xyprintf(j * 150, i * 150, "%d", num);
		}

	}
}
void bk() {
	PIMAGE bg = newimage();
	getimage(bg, "bk.png");
	putimage(0, 0, 600, 600, bg, 0, 0, getwidth(bg), getheight(bg));
	delimage(bg);
}
//根据 four_dim_arr 中的数字，在九宫格上绘制连线
void graph() {
	for (int i = 0; i < 4; ++i) {
		for (int j = 0; j < 4; ++j) {
			int index1 = four_dim_arr[i][j] - 1;
			
			if (index1 < 0 || index1 >= 9) {
				cout << "Invalid index for point 1!" << endl;
				continue;
			}
			
			int* point1 = rw[index1];
			
			// 打印当前点的信息
			cout << "Point " << j+1 << " key: (" << four_dim_arr[i][j] << ") values: (" << point1[0] << ", " << point1[1] << ")" << endl;
			
			// 如果不是最后一个点，则连线到下一个点并打印下一个点的信息
			if (j < 3) {
				int index2 = four_dim_arr[i][j + 1] - 1;
				
				if (index2 < 0 || index2 >= 9) {
					cout << "Invalid index for point 2!" << endl;
					continue;
				}
				
				int* point2 = rw[index2];
				
				// 打印下一个点的信息
				cout << "Point " << j+2 << " key: (" << four_dim_arr[i][j+1] << ") values: (" << point2[0] << ", " << point2[1] << ")" << endl;
				
				int x = point1[0];
				int y = point1[1];
				int xx = point2[0];
				int yy = point2[1];
				
				settextjustify(CENTER_TEXT, CENTER_TEXT);
				xyprintf(2 * 150, 50, "%d", arr[i]);
				line(x, y, xx, yy);
			}
		}
		
		nine(); // 重绘9个圆，覆盖线
		delay_s(1);
		cout << endl;
		cleardevice();
		bk();
	}
}

int main() {
	randomize();
	initgraph( 600, 600 );
	setcolor( RGB(0xff, 0xff, 0xff) );
	setfillcolor(0xFF64DFDF);
	setrendermode(RENDER_MANUAL);

	enu();//分配坐标点
	split();//拆分4个点
	bk();//初始化背景
	nine();//初始化9个圆
	for ( ; is_run(); delay_fps(60) ) {
		graph();
	}
	closegraph();
	return 0;
}
